package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/?envType=study-plan-v2&envId=top-100-liked
 * 动态规划 自我AC
 *
 * @author yulongTian
 * @create 2024-07-15 13:35
 */
public class Test73 {
    public static void main(String[] args) {
        int[] prices = {1, 2};
        System.out.println(new Test73().maxProfit(prices));

    }

    public int maxProfit(int[] prices) {
        if (prices.length == 1) {
            return 0;
        }
        int[] dp = new int[prices.length - 1];
        for (int i = 1; i < prices.length; i++) {
            dp[i - 1] = prices[i] - prices[i - 1];
        }
        int[] dpp = new int[prices.length - 1];
        int res = 0;
        dpp[0] = dp[0];
        res = Math.max(res, dpp[0]);
        for (int i = 1; i < dp.length; i++) {
            dpp[i] = Math.max(dp[i], dp[i] + dpp[i - 1]);
            res = Math.max(res, dpp[i]);
        }

        return res;
    }

}
